
<link rel="stylesheet" href="{{URL::asset('/user/css/security.css')}}">

<!-- 导航 start -->
<div class="j_security">
    <div class="j_security_title">
        <h6>账户安全</h6>
        <div id="UserDetail" class="mysettings-packup">
            <dl class="basic">
                <dd class="basic-wide">当前密码</dd>
                <dd class="fl">
                    <input class="mysettings-input" name="username" type="password" id="j_user_password"  placeholder="******" ><!--disabled=""-->

                </dd>
                <dd class="fl ml30 mt10" id="j_pwd_tishi">

                </dd>
            </dl>
            <dl class="basic" style="margin-bottom:0px;">
                <dd class="basic-wide">新密码</dd>
                <dd class="fl">
                    <input class="mysettings-input" type="password" id="j_newpwd"  maxlength="16" placeholder="******"><!--disabled=""-->

                </dd>
                <dd class="fl ml30 mt10" id="msgNewPassWord">
                    请使用8-16位字母(区分大小写)、数字、特殊字符，建议组合使用
                </dd>
            </dl>
            <div class="fl ml100" style="margin-bottom:20px;clear: both;display: block;">
                <p id="low" class="j_password_min j_margin_box">低</p>
                <p id="middle" class="j_password_min">中</p>
                <p id="High" class="j_password_min">高</p>
            </div>
            <dl class="basic">
                <dd class="basic-wide">确认密码</dd>
                <dd class="fl">
                    <input class="mysettings-input" type="password" id="j_affirm_pwd"  maxlength="20" placeholder=""><!--disabled=""-->

                </dd>
                <dd class="fl ml30 mt10 j_email_l" id="msgReNewPassWord">
                    请再次输入新密码
                </dd>
            </dl>
            
            <div class="mysettings-headfl ml100 mt5" style="clear: both;">
                <a href="javascript:;">
                    <p id="j_submit" class="userset-but-r" style="margin-top:38px;">提交</p>
                </a>

            </div>
        </div>
    </div>
</div>
<!--导航 end-->
<script>
    $(document).ready(function () {
        $("#j_newpwd").focus(function () {
            $("#j_newpwd").removeClass("j_border");
            $("#msgNewPassWord").show();
        });
        $("#j_user_password").focus(function () {
            $("#j_user_password").removeClass("j_border");
        });
        $("#j_affirm_pwd").focus(function () {
            $("#j_affirm_pwd").removeClass("j_border");
            $("#msgReNewPassWord").show();
        });
        $("#j_newpwd").keyup(function () {
            var res = 0;
            var newPassWord = $("#j_newpwd").val();
            res = j_computeComplex(newPassWord);
            if (res == 0) {
                clearComplex();
            }
            if (res > 0 && res <= 18) {
                $("#low").removeClass("j_active");
                $("#middle").removeClass("j_active");
                $("#High").removeClass("j_active");
                $("#middle").removeClass("j_active");
                $("#High").removeClass("j_active");
                $("#low").addClass("j_active");
            }
            if (res > 18 && res <= 30) {
                $("#low").removeClass("j_active");
                $("#middle").removeClass("j_active");
                $("#High").removeClass("j_active");
                $("#High").removeClass("j_active");
                $("#low").addClass("j_active");
                $("#middle").addClass("j_active");
            }
            if (res > 30) {
                $("#low").removeClass("j_active");
                $("#middle").removeClass("j_active");
                $("#High").removeClass("j_active");
                $("#low").addClass("j_active");
                $("#middle").addClass("j_active");
                $("#High").addClass("j_active");
            }
        });

        $("#j_submit").click(function () {
            $("#j_newpwd").removeClass("j_border");
            $("#j_user_password").removeClass("j_border");
            $("#j_affirm_pwd").removeClass("j_border");
            var newPassWord = $("#j_newpwd").val();
            var j_user_password = $("#j_user_password").val();
            var reNewPassWord = $("#j_affirm_pwd").val();
            if (j_user_password.length < 8 || j_user_password.length > 16) {
                $("#j_user_password").addClass("j_border");
                layer.tips('密码长度为8-16位','#j_user_password',{time:1000});
                return;
            }
            if (newPassWord.length < 8 || newPassWord.length > 16) {
                layer.tips('密码长度为8-16位','#j_newpwd',{time:1000});
                $("#msgNewPassWord").hide();
                return;
            }
            if (reNewPassWord.length < 8 || reNewPassWord.length > 16) {
                $("#j_affirm_pwd").addClass("j_border");
                layer.tips('密码长度为8-16位','#j_affirm_pwd',{time:1000});
                return;
            }
            if(j_user_password == newPassWord){
                $("#msgNewPassWord").text('');
                layer.tips('原密码与现密码重复','#j_newpwd',{time:1000});
                return;
            }else{
                $("#msgNewPassWord").text('');
                $("#msgReNewPassWord").text('');
            }
            if (newPassWord != reNewPassWord) {
                $("#j_newpwd").addClass("j_border");
                $("#j_affirm_pwd").addClass("j_border");
                $("#msgNewPassWord").text('');
                $("#msgReNewPassWord").text('');
                layer.tips('两次密码不一致','#j_affirm_pwd',{time:1000});
                return;
            }else{
                $("#msgNewPassWord").text('');
                $("#msgReNewPassWord").text('');
            }
            var updatePassUrl = "{{ url('/updatepwd')}}";
            $.ajax({
                type: "POST",
                data: { j_user_password: j_user_password, newPassWord: newPassWord },
                url: updatePassUrl,
                dataType: "json",
                headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                success:function (d) {
                    if (d.status == 1) {
                        clearComplex();
                        $("#j_newpwd").val('');
                        $("#j_user_password").val('');
                        $("#j_affirm_pwd").val('');
                        layer.msg('修改成功',{time:1000});
                    } else if (d.status == 3) {
                        
                        layer.tips('','#downWord',{time:1000});
                    } else if (d.status == 2) {
                        $("#j_user_password").addClass("j_border");
                        $("#j_pwd_tishi").text("原密码错误!");
                    } else {
                        layer.msg('修改失败',{time:1000});
                    }
                }
            });
        });
    });
    function clearComplex() {
        $("#low").removeClass("j_active");
        $("#middle").removeClass("j_active");
        $("#High").removeClass("j_active");
        $("#low").addClass("j_active");
        $("#middle").addClass("j_active");
        $("#High").addClass("j_active");
    }

    function clear() {
        $("#j_newpwd").val("");
        $("#j_user_password").val("");
        $("#j_affirm_pwd").val("");
    }
     function j_computeComplex(password) {
        var complex = 0;
        var length = password.length;
        var pre = '';
        var preType = 0;
        for (var i = 0; i < length; i++) {
            var cur = password.charAt(i);
            var curType = j_gettype(password, i);
            if (preType != curType || !j_isregular(cur, pre, curType)) {
                complex += curType + j_getcomplex(curType, preType);
            }
            pre = cur;
            preType = curType;
        }
        return complex;
    }

    function j_gettype(str, i) {
        if (str.charCodeAt(i) >= 48 && str.charCodeAt(i) <= 57) {
            return 1;
        }
        else if (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) {
            return 2;
        }
        else if (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) {
            return 3;
        }
        return 4;
    }

    function j_isregular(cur, pre, type) {
        var curCode = cur.charCodeAt(0);
        var preCode = pre.charCodeAt(0);
        if (curCode - preCode == 0) {
            return true;
        }
        if (type != 4 && (curCode - preCode == 1 || curCode - preCode == -1)) {
            return true;
        }
        return false;
    }

    function j_getcomplex(curType, preType) {
        if (preType == 0 || curType == preType) {
            return 0;
        } else if (curType == 4 || preType == 4) {
            return 2;
        } else {
            return 1;
        }
    }


</script>